package com.ciaojian.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ciaojian.core.model.UserMember;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;

/**
 * <p>
 * 用户和会员卡关联表 Mapper 接口
 * </p>
 *
 * @author Atlakyn
 * @since 2021-04-07
 */
public interface UserMemberMapper extends BaseMapper<UserMember> {
    /**
     * 获取用户会员卡信息
     *
     * @param userId 用户ID
     * @return 会员卡信息
     */
    Map<String, Object> getByUserId(@Param("userId") Integer userId);

    /**
     * 查询用户是否购买某张会员卡
     *
     * @param userId   用户 id
     * @param memberId 会员 id
     * @return 用户会员信息
     */
    @Select("SELECT member_id, user_id FROM d_user_member WHERE member_id = #{memberId} AND user_id = #{userId}")
    UserMember getUserMember(Integer userId, Integer memberId);

    /**
     * 更新用户的会员卡类型
     *
     * @param userId   用户
     * @param memberId 会员卡
     */
    @Update("UPDATE d_user_member SET member_id = #{memberId} WHERE user_id = #{userId}")
    void updateMemberIdByUserId(@Param("userId") Integer userId, @Param("memberId") Integer memberId);
}
